package com.wayful.minedetect.controller;

import com.wayful.minedetect.LoginApp;
import com.wayful.minedetect.service.AnalysisService;
import com.wayful.minedetect.utils.ServiceFactory;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;

import java.net.URL;
import java.util.ResourceBundle;

public class DefaultController implements Initializable {
    @FXML
    private ImageView bookImg;
    @FXML
    private Label typeCount, bookCount, readerCount, adminCount;
    @FXML
    private WebView mapContainer;
    private AnalysisService analysisService = ServiceFactory.getAnalysisServiceInstance();

    //轮播图资源数组
    String[] imgPath = {"book1.jpg", "book2.jpg", "book3.jpg", "book4.jpg", "book5.jpg"};

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //获取后台的统计数据，显示在相应区域
        typeCount.setText("市场类型" + analysisService.getTypesCount() + "种");
        bookCount.setText("经营单位" + analysisService.getBooksCount() + "个");
        readerCount.setText("执法人员" + analysisService.getReadersCount() + "人");
        adminCount.setText("管理员" + analysisService.getAdminsCount() + "个");
        
        String url = LoginApp.class.getResource("/html/NewFile.html").toExternalForm();

        mapContainer.getEngine().load(url);
        
//        //新建一个线程，用来轮播
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    //循环读取图片数组
//                    for (int i = 0; i < imgPath.length; i++) {
//                        //用每个资源创建一个图片对象
//                        Image image = new Image("/img/" + imgPath[0]);
//                        //开启一个UI线程
//                        Platform.runLater(new Runnable() {
//                            @Override
//                            public void run() {
//                                //将创建的Image对象设置给ImageView
//                                bookImg.setImage(image);
//                            }
//                        });
//                        try {
//                            //休眠2秒
//                            Thread.sleep(2000);
//                        } catch (InterruptedException e) {
//                            e.printStackTrace();
//                        }
//                        //轮播到最后一张图的时候，回到第一张重新播放
//                        if (i == imgPath.length - 1) {
//                            i = 0;
//                        }
//                    }
//                }
//            }
//        }).start();
    }
}
